package cn.hist.hfp.converter;

import cn.hist.hfp.entity.Commodity;
import cn.hist.hfp.vo.CommodityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class CommodityConverter {

    private final CommodityProducerConverter commodityProducerConverter;

    @Autowired
    public CommodityConverter(CommodityProducerConverter commodityProducerConverter) {
        this.commodityProducerConverter = commodityProducerConverter;
    }

    public CommodityVO converter(Commodity commodity) {
        return CommodityVO.builder()
                .description(commodity.getDescription())
                .id(commodity.getId())
                .image(commodity.getImage())
                .name(commodity.getName())
                .price(commodity.getPrice())
                .type(commodity.getType())
                .producer(commodityProducerConverter.converter(commodity.getProducer())).build();
    }

    public List<CommodityVO> converter(List<Commodity> commodityList) {
        List<CommodityVO> commodityVOS = new ArrayList<>();
        for (Commodity commodity: commodityList) {
            commodityVOS.add(converter(commodity));
        }
        return commodityVOS;
    }

    public Set<CommodityVO> converter(Set<Commodity> commodities) {
        Set<CommodityVO> commodityVOS = new HashSet<>();
        for (Commodity commodity: commodities) {
            commodityVOS.add(converter(commodity));
        }
        return commodityVOS;
    }
}
